<template>
  <div class="footInput">
    <div class="deactive" v-if="!isShowTextarea">
      <input
        v-model="content"
        type="text"
        class="writeComment"
        placeholder="写跟帖"
        @focus="showTextarea"
      />
      <div class="comment">
        <span class="iconfont iconpinglun-"></span>
        <div class="nums">1020</div>
      </div>
      <span class="iconfont iconshoucang"></span>
      <span class="iconfont iconfenxiang"></span>
    </div>
    <div class="active" v-if="isShowTextarea">
      <textarea
        v-model="content"
        ref="textDom"
        rows="3"
        class="writeComment"
        @blur="hideTextarea"
      ></textarea>
      <div class="sendBtn" @click="sendComment">发送</div>
    </div>
  </div>
</template>

<script>
import eventBus from "../../utils/eventBus";
export default {
  data() {
    return {
      isShowTextarea: false,
      content: "",
      parentID: "",
    };
  },
  components: {
    eventBus,
  },
  mounted() {
    //页面渲染完，利用事件总线监听另一个兄弟组件的点击事件
    eventBus.$on("sendMsg", (parentID) => {
      this.isShowTextarea = true;
      this.$nextTick(() => {
        this.$refs.textDom.focus();
      });
      this.parentID = parentID;
    });
  },
  destroyed() {
    //   用了事件总线监听，在记得销毁组件的时候，清楚监听
    eventBus.$off();
  },
  methods: {
    showTextarea() {
      this.isShowTextarea = true;
      // 显示之后要聚焦，以便下一个操作失去焦点的时候隐藏输入框
      //因为计算机执行下面代码的时候，textDom对应的多行文本框还没出来，所以要设延时器
      //   setTimeout(() => {
      //     this.$refs.textDom.focus();
      //   }, 100);
      // 这种方法虽然可以，但是只能猜大概什么时候执行完上边的代码，vue中有一种方法能在下次渲染完毕后再执行
      this.$nextTick(() => {
        this.$refs.textDom.focus();
      });
    },
    hideTextarea() {
      // 因为点击发送评论的时候会发生先隐藏文本框导致发送失败请求的情况所以设置给隐藏文本框定时器
      setTimeout(() => {
        this.isShowTextarea = false;
        // 如果按父评论的回复会带上parentID,但是如果用户按错了或者不想回复父评论的，就要把ParentID清掉，不影响其他的操作
        this.parentID = "";
      }, 100);
    },
    sendComment() {
      this.$axios({
        url: "/post_comment/" + this.$route.params.id,
        method: "post",
        data: {
          parent_id: this.parentID,
          content: this.content,
        },
      }).then((res) => {
        // console.log(res.data);
        this.content = "";
        this.$emit("reloadComment");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.footInput {
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: #f2f2f2;
  width: 100%;
  .deactive {
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    height: 50 /360 * 100vw;
    .writeComment {
      width: 180 /360 * 100vw;
      height: 30 /360 * 100vw;
      border-radius: 15/360 * 100vw;
      background-color: #d7d7d7;
      padding: 0 20/360 * 100vw;
      box-sizing: border-box;
      border: none;
      outline: none;
    }
    .comment {
      position: relative;
      .nums {
        position: absolute;
        top: -5px;
        right: -16px;
        font-size: 4/360 * 100vw;
        height: 18/360 * 100vw;
        line-height: 18/360 * 100vw;
        background-color: red;
        padding: 0 4/360 * 100vw;
        border-radius: 12/360 * 100vw;
        color: #fff;
      }
      .iconpinglun- {
        font-size: 23/360 * 100vw;
      }
    }
    .iconshoucang {
      font-size: 23/360 * 100vw;
    }
    .iconfenxiang {
      font-size: 22/360 * 100vw;
    }
  }
  .active {
    display: flex;
    align-items: flex-end;
    padding: 10/360 * 100vw;
    .writeComment {
      width: 260 /360 * 100vw;
      height: 90 /360 * 100vw;
      background-color: #d7d7d7;
      border-radius: 14/360 * 100vw;
      padding: 16/360 * 100vw;
      box-sizing: border-box;
      resize: none;
      border: none;
      outline: none;
    }
    .sendBtn {
      font-size: 16/360 * 100vw;
      height: 30/360 * 100vw;
      line-height: 30/360 * 100vw;
      background-color: red;
      color: #fff;
      border-radius: 18/360 * 100vw;
      padding: 0 16/360 * 100vw;
      margin-left: 16/360 * 100vw;
    }
  }
}
</style>